package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.UserAlias;
import com.qinggeng.project.system.mapper.UserAliasMapper;
import com.qinggeng.project.system.service.IUserAliasService;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserAliasServiceImpl implements IUserAliasService {

    @Resource
    private UserAliasMapper userAliasMapper;

    @Override
    public int insertUserAlias(UserAlias alias) {
        return userAliasMapper.insertUserAlias(alias);
    }
    @Override
    public UserAlias selectMemberUserAliasByUserId(Long userId){
        return userAliasMapper.selectMemberUserAliasByUserId(userId);
    }
    @Override
    public UserAlias selectUserAliasByAlias(String alias) {
        return userAliasMapper.selectUserAliasByAlias(alias);
    }

    @Override
    public List<UserAlias> selectUserAliasList(UserAlias where) {
        return userAliasMapper.selectUserAliasList(where);
    }

    @Override
    public int updateUserAlias(UserAlias alias) {
        return userAliasMapper.updateUserAlias(alias);
    }

    @Override
    public int deleteUserAliasById(Long id) {
        return userAliasMapper.deleteUserAliasById(id);
    }

    @Override
    public int setMemberUserAlias(String alias,Long userId,String token) {
        UserAlias userAlias=this.selectMemberUserAliasByUserId(userId);
        //已绑定用户信息（登录状态下）
        if(userAlias!=null){
            return this.updateLoginUserAlias(userAlias,alias,token);
        }
        return this.setLoginUserAlias(alias,userId,UserTypeEnum.MEMBER.getCode(),token);
    }

    @Override
    public int setCourierUserAlias(String alias,Long userId,String token){
        /*if(StringUtils.isEmpty(alias)){
            throw new CustomException("别名不能为空");
        }*/
        return this.setLoginUserAlias(alias, userId, UserTypeEnum.COURIER.getCode(),token);
    }

   @Override
    public int setLoginUserAlias(String alias,Long userId,String userType,String token){
        //别名为空
        if(StringUtils.isEmpty(alias)){
            UserAlias newAlias=new UserAlias(userId,userType,null,token);
            return this.insertUserAlias(newAlias);
        }
        UserAlias userAlias=selectUserAliasByAlias(alias);
        if(null==userAlias){                ;
            this.insertUserAlias(userAlias=new UserAlias(alias));
        }
        userAlias.setUserId(userId);
        userAlias.setUserType(userType);
        userAlias.setToken(token);
        return this.updateUserAlias(userAlias);
    }

    @Override
    public int updateLoginUserAlias(UserAlias userAlias,String alias,String token){
        String userOldAlias="";
        if(StringUtils.isNotEmpty(alias)){
            //获取用户旧的推送别名
            if(StringUtils.isNotEmpty(userAlias.getAlias())){
                userOldAlias=userAlias.getAlias();
            }
            if(!userOldAlias.equals(alias)){
                //删除重名的别名
                UserAlias oldAlias=selectUserAliasByAlias(alias);
                if(oldAlias!=null){
                    deleteUserAliasById(oldAlias.getId());
                }
                //设置登录用户的新别名
                userAlias.setAlias(alias);
            }
        }
        //更新token令牌
        String oldToken="";
        if(StringUtils.isNotEmpty(token)){
            if(userAlias.getToken()!=null){
                oldToken=userAlias.getToken();
            }
            if(!oldToken.equals(token)){
                userAlias.setToken(token);
            }
        }
        int rows=updateUserAlias(userAlias);
        /**
         * 【注意位置，要放在更新之后】
         * 将登录用户的旧别名转存为未登录用户的别名
         */
        if(StringUtils.isNotEmpty(userOldAlias)&&selectUserAliasByAlias(userOldAlias)==null){
            insertUserAlias(new UserAlias(userOldAlias));
        }
        return rows;
    }

    @Override
    public List<UserAlias> selectCourierUserAliasListByOrdersId(Long ordersId) {
        return userAliasMapper.selectCourierUserAliasListByOrdersId(ordersId);
    }


}
